//
//  SNSCropImageViewController.m
//  iBaby365v2
//
//  Created by Kirinrin on 14-8-4.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "SNSCropImageViewController.h"
#import <HFImageEditorViewController+Private.h>

@interface SNSCropImageViewController ()
@property (weak, nonatomic) IBOutlet UIBarButtonItem *orginBution;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *cropButtion;

@end

@implementation SNSCropImageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.cropRect = CGRectMake(0, 0, 320, 320);
        self.minimumScale = 0.2;
        self.maximumScale = 10;
        self.checkBounds = YES;
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil cropRect:(CGRect)rect
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.cropRect = rect;
        self.minimumScale = 0.2;
        self.maximumScale = 10;
        self.checkBounds = YES;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if (self.noOptionButton) {
        self.orginBution.enabled = NO;
        self.cropButtion.enabled = NO;
    }
//    self.cropRect = _customRect;
//    self.cropSize = _customRect.size;
//    self.minimumScale = 0.2;
//    self.maximumScale = 5;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)cropImageToFit:(id)sender {
    self.cropRect = CGRectMake((self.frameView.frame.size.width-320)/2.0f, (self.frameView.frame.size.height-320)/2.0f, 320, 320);
    [self reset:YES];
}
- (IBAction)orignalImage:(id)sender {
    CGRect frameRect = self.frameView.frame;
    CGSize imageSize = self.sourceImage.size;
    
    
    CGFloat frameScale = frameRect.size.width/frameRect.size.height;
    CGFloat imageScale = imageSize.width/imageSize.height;
    
    CGRect newRect;
    CGSize newImageSize;
    if (imageScale < frameScale) {
        //竖着
        newImageSize = CGSizeMake(imageSize.width/imageSize.height * frameRect.size.height, frameRect.size.height);
        
    }else{
        //横着
        newImageSize = CGSizeMake(frameRect.size.width, imageSize.height/imageSize.width * frameRect.size.width);
        
    }
    newRect = CGRectMake(abs(frameRect.size.width - newImageSize.width)/2, abs(frameRect.size.height-newImageSize.height)/2, newImageSize.width, newImageSize.height);
    
    self.cropRect = newRect;
    [self reset:YES];
}

@end
